package com.purong.purong.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.purong.purong.Contant;
import com.purong.purong.PuRongApp;
import com.purong.purong.R;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.util.SharePreUtil;
import com.yao.sdk.net.ResponseInfo;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by zengwenjie on 2017/12/6.
 */
public class IntentionActivity extends BaseActivity {
    private ImageView topImg;
    private TextView confirm;
    private EditText nameEd,phoneEd,moneyEd,noteEd;
    private boolean nameInput,phoneInput,moneyInput,noteInput,hasLogin;
    @Override
    public int getRootViewId() {
        return R.layout.activity_intention;
    }

    @Override
    public void initView() {
        showBack();
        showTitle("贷款意向");
        hasLogin=!(PuRongApp.getLoginInfo(this)==null);
        if (!hasLogin){
            findViewById(R.id.name_lay).setVisibility(View.VISIBLE);
            findViewById(R.id.name_cut_line).setVisibility(View.VISIBLE);
            findViewById(R.id.phone_lay).setVisibility(View.VISIBLE);
            findViewById(R.id.phone_cut_line).setVisibility(View.VISIBLE);
        }
        confirm=(TextView)findViewById(R.id.confirm);
        topImg=(ImageView)findViewById(R.id.top_img);
        topImg.setLayoutParams(new LinearLayout.LayoutParams( CommonUtil.getScreenWidth(this), CommonUtil.getScreenWidth(this)/3));
        nameEd=(EditText)findViewById(R.id.name_ed);
        phoneEd=(EditText)findViewById(R.id.phone_ed);
        moneyEd=(EditText)findViewById(R.id.money_ed);
        noteEd=(EditText)findViewById(R.id.note_ed);
        nameEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    nameInput = false;
                } else {
                    nameInput = true;
                }
                if (hasLogin){
                    if (moneyInput) {
                        confirm.setBackgroundColor(getResources().getColor(R.color.blue));
                    } else {
                        confirm.setBackgroundColor(getResources().getColor(R.color.button_gray));
                    }
                }else {
                    if (nameInput&&phoneInput&&moneyInput) {
                        confirm.setBackgroundColor(getResources().getColor(R.color.blue));
                    } else {
                        confirm.setBackgroundColor(getResources().getColor(R.color.button_gray));
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        phoneEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    phoneInput = false;
                } else {
                    phoneInput = true;
                }
                if (hasLogin){
                    if (moneyInput) {
                        confirm.setBackgroundColor(getResources().getColor(R.color.blue));
                    } else {
                        confirm.setBackgroundColor(getResources().getColor(R.color.button_gray));
                    }
                }else {
                    if (nameInput&&phoneInput&&moneyInput) {
                        confirm.setBackgroundColor(getResources().getColor(R.color.blue));
                    } else {
                        confirm.setBackgroundColor(getResources().getColor(R.color.button_gray));
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        moneyEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (s.toString().contains(".")) {
                    if (s.toString().startsWith(".")) {
                        moneyEd.setText(s.toString().replaceFirst(".",""));
                    }else if (s.length() - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(0,
                                s.toString().indexOf(".") + 3);
                        moneyEd.setText(s);
                        moneyEd.setSelection(moneyEd.getText().toString().trim().length());
                    }
                }

                if (s.toString().startsWith("0")
                        && s.toString().trim().length() > 1) {
                    if (!s.toString().substring(1, 2).equals(".")) {
                        moneyEd.setText(s.toString().replaceFirst("0",""));
                        moneyEd.setSelection(moneyEd.getText().toString().trim().length());
                    }
                }
                if (moneyEd.getText().toString().trim().equals("")) {
                    moneyInput = false;
                } else {
                    moneyInput = true;
                }
                if (hasLogin){
                    if (moneyInput) {
                        confirm.setBackgroundColor(getResources().getColor(R.color.blue));
                    } else {
                        confirm.setBackgroundColor(getResources().getColor(R.color.button_gray));
                    }
                }else {
                    if (nameInput&&phoneInput&&moneyInput) {
                        confirm.setBackgroundColor(getResources().getColor(R.color.blue));
                    } else {
                        confirm.setBackgroundColor(getResources().getColor(R.color.button_gray));
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        noteEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    noteInput = false;
                } else {
                    noteInput = true;
                }
//                if (hasLogin){
//                    if (moneyInput&&noteInput) {
//                        confirm.setBackgroundColor(getResources().getColor(R.color.blue));
//                    } else {
//                        confirm.setBackgroundColor(getResources().getColor(R.color.button_gray));
//                    }
//                }else {
//                    if (nameInput&&phoneInput&&moneyInput&&noteInput) {
//                        confirm.setBackgroundColor(getResources().getColor(R.color.blue));
//                    } else {
//                        confirm.setBackgroundColor(getResources().getColor(R.color.button_gray));
//                    }
//                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.confirm:
                if (!hasLogin){
                    if (!nameInput){
                        showToast("请填写姓名");
                        return;
                    }
                    if (!phoneInput) {
                        showToast("请填写电话");
                        return;
                    }
                }

                if (!moneyInput){
                    showToast("请填写贷款金额");
                    return;
                }
                try{
                    if (Double.valueOf(moneyEd.getText().toString().trim())==0){
                        showToast("贷款金额不能为0");
                        return;
                    }
                }catch (Exception e){
                    showToast("贷款金额不能为0");
                    return;
                }

                setIntention();
                break;
        }
    }
    private void setIntention(){
        Map<String, String> params = new LinkedHashMap<String, String>();
        if (!hasLogin){
            params.put("name",nameEd.getText().toString().trim());
            params.put("phone",phoneEd.getText().toString().trim());
        }
        params.put("province_id", SharePreUtil.getInstance(this).getLoactionParentId());
        params.put("city_id", SharePreUtil.getInstance(this).getLoactionAreaId());
        params.put("loan_money",moneyEd.getText().toString().trim());
        if (noteInput){
            params.put("note",noteEd.getText().toString().trim());
        }
        request(Contant.LOAN_INTENTION, params, true,true);
    }

    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.LOAN_INTENTION)){
            if (responseInfo.isRequestOk){
                showToast("已提交");
                this.finish();
            }
        }
    }

    @Override
    public void initData() {

    }
}
